﻿#region Copyright Notice
// This file is part of GestionPedago.
// <description>
// Copyright (C) 2012 ENI Ecole Informatique
// 
// GestionPedago is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// <program> is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GestionPedago.EntityContext;
using GestionPedago.EntityContext.EntityOPTools;

namespace GestionPedago.Persistence.OPManager
{
   public abstract class ValidationCompetenceOPManager
    {
       public static List<ValidationCompetence> GetListeValidationCompetenceByIdPlanningECFDetail(int idPlanningECFDetail)
       {
           try
           {
               IQueryable<ValidationCompetence> query = EntityConnection.GetContext().ValidationCompetence;

               query = query.Where(it => it.IdPlanningECFDetail == idPlanningECFDetail);

               return query.ToList();
           }
           catch (Exception ex)
           {
               throw ex;
           }
       }

       public static ValidationCompetence GetValidationCompetenceById(int id)
       {

           try
           {
               var query = from it in EntityConnection.GetContext().ValidationCompetence
                           where it.IdValidationCompetence == id
                           select it;

               ValidationCompetence c = null;

               c = query.First();

               return c;

           }
           catch (Exception ex)
           {
               throw ex;
           }

       }


       public static ValidationCompetence AjouterValidationCompetence(ValidationCompetence c)
       {
           try
           {
               if (c.IdCompetence != 0)
               {
                   c.Competence = CompetenceOPManager.GetCompetenceById(c.IdCompetence);
               }
               if (c.Competence != null && !EntityStateChecker.IsDetached(c.Competence))
               {
                   EntityConnection.GetContext().Competence.Detach(c.Competence);
               }
              c.PlanningECFDetail = PlanningECFDetailOPManager.GetPlanningECFDetailById(c.IdPlanningECFDetail);
               if (c.PlanningECFDetail != null && !EntityStateChecker.IsDetached(c.PlanningECFDetail))
               {
                   EntityConnection.GetContext().PlanningECFDetail.Detach(c.PlanningECFDetail);
               }
               if (!EntityStateChecker.IsDetached(c))
               {
                   EntityConnection.GetContext().ValidationCompetence.Detach(c);
               }
               EntityConnection.GetContext().ValidationCompetence.AddObject(c);
               EntityConnection.GetContext().SaveChanges();
               EntityConnection.ClearEntities();
               return c;
           }
           catch (Exception ex)
           {
               throw ex;
           }
       }

       public static void SupprimerValidationCompetence(ValidationCompetence vc)
       {
           try
           {
              // vc = ValidationCompetenceOPManager.GetValidationCompetenceById(vc.IdValidationCompetence);
               
               EntityConnection.GetContext().ValidationCompetence.DeleteObject(vc);
               EntityConnection.GetContext().SaveChanges();
           }
           catch (Exception ex)
           {
               throw ex;
           }

       }
    }
}
